<?php
class ManageImageForm extends Form
{
	function ManageImageForm(){
//		echo "dung tai day dau ham"; exit();
		CGlobal::$website_title="Quản trị Ảnh sản phẩm";
		if(Url::get('cmd')=='delete'){
			$this->delete_images(Url::get('id'));																											
			Url::redirect_url(Url::build_all(array('chk_id','del_all','cmd','id')));
		}
		
		Form::Form('ManageImageForm');		
		$this->link_css('style/manage_item.css');
		$this->link_css('style/aznet/ui.datepicker.css');		
		$this->link_js('javascript/jquery/ui.datepicker.js');
		$this->link_js('javascript/jquery/packed/jquery.lightbox.js');
		$this->link_css('style/aznet/jquery.lightbox.css');
	}
	
	static function delete_images($id){
		$row = DB::select('item_image','id='.$id);
		if($row){
			if($row['root_id']){//Ảnh gốc
				DB::delete('item_image','root_id='.$row['root_id']);	
			}
			else{
				DB::delete('item_image','id='.$id);	
			}
			
			//update memcache IMG
			$sql = "SELECT item_id FROM item_image WHERE (id = $id OR root_id = $id) ". (User::have_permit(ADMIN_IMAGE)?'':" AND user_id=".User::id());
			$re = DB::query($sql);
			
			if($re){
				$item = mysql_fetch_assoc($re);										
				if($item["item_id"]){
					require_once ROOT_PATH.'core/Item.php';	
					Item::get_item_images($item["item_id"],1);						
				}					
			}
			//update memcache IMG
			
			if($row['img_url']){
				if(DB::query("UPDATE item SET img_url='' WHERE img_url='".$row['img_url']."'") && MEMCACHE_ON){
					$sql 	= "SELECT * FROM item WHERE img_url = '{$row['img_url']}'";
					$re 	= DB::query($sql);
					
					while($item_memcache = mysql_fetch_assoc($re)){
						$item_memcache['img_server'] = 0;
						$item_memcache['img_url']= '';
						AZMemcache::do_put("item:".$item_memcache['id'],$item_memcache);
					}
				}
				
				AZLib::ftp_image_delete_file($row['img_url'],$row['img_server']);
			}
		}
	}
	
	function on_submit(){
		$ids = (isset($_POST['chk_id']))?$_POST['chk_id']:array(); 
							
		if(count($ids)>0){
			for($i=0;$i<count($ids);$i++){				
				$this->delete_images($ids[$i]);				
			}
			Url::redirect_url(Url::build_all(array('chk_id','del_all','cmd','id')));
		}	
	}
	
	function draw(){
		$this->beginForm();
		global $display;
		
		
		$search_value='';
		
		$display->add('up_checked','checked="checked"');
		
		if(Url::get('root_img')){
			$search_value.=($search_value!=''?' AND ':' WHERE').' root_id=0 ';
			
			$display->add('root_img_checked','checked="checked"');
		}
		else{
			//$search_value.=($search_value!=''?' AND ':' WHERE').' img.root_id>0 ';
			$display->add('root_img_checked','');
		}
		
		if(Url::get('no_item_img')){
			$search_value.=($search_value!=''?' AND ':' WHERE').' item_id=0 ';
			
			$display->add('no_item_img_checked','checked="checked"');
		}
		else{
			//$search_value.=($search_value!=''?' AND ':' WHERE').' root_id>0 ';
			$display->add('no_item_img_checked','');
		}
		
		// search ô textbox	
		if(Url::get('user_name')!=''){				
			$search_value .= ($search_value!=''?' AND ':' WHERE').' user_name LIKE "%'.Url::get('user_name').'%" ';
		}		
		
		//search theo ngay thang nam
		$created_time_from=0;
		$created_time_to=0;
		$current_time_from = null;
		$current_time_to = null;
		if(Url::get('created_time_from')){
			$current_time_from = Url::get('created_time_from');
			$date_arr = explode('-',$current_time_from);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		if(Url::get('created_time_to')){
			$current_time_to = Url::get('created_time_to');
			$date_arr = explode('-',$current_time_to);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
		}
		
		if($created_time_from){																	
			$search_value .= ($search_value!=''?' AND ':' WHERE').' time >= '.$created_time_from;
		}
		if($created_time_to){																	
			$search_value .= ($search_value!=''?' AND ':' WHERE').' time <= '.$created_time_to;
		}
		
		if($search_value == ''){
			
			//create current date
			$date = Date('d-m-Y');
		
			$current_time_from = $date;
			$date_arr = explode('-',$current_time_from);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_from = mktime(0,0,0,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
			
			$current_time_to = $date;
			$date_arr = explode('-',$current_time_to);
			if(isset($date_arr[0]) && isset($date_arr[1]) && isset($date_arr[2]))
			$created_time_to = mktime(23,59,59,(int)$date_arr[1],(int)$date_arr[0],(int)$date_arr[2]);
			
			if($created_time_from){																	
				$search_value .= ($search_value!=''?' AND ':' WHERE').' time >= '.$created_time_from;
			}
			if($created_time_to){																	
				$search_value .= ($search_value!=''?' AND ':' WHERE').' time <= '.$created_time_to;
			}
		}
		
		$display->add('created_time_from',$current_time_from);
		$display->add('created_time_to',$current_time_to);
		
		$item_per_page = 120;
		
		$sql_count='SELECT count(*) AS total_img FROM item_image '.$search_value;
		
		$total=DB::fetch($sql_count,'total_img',0);
		
		$items=array();
		
		if($total){
			$limit='';
				
			$paging = AZPagging::pagingSE($limit,$total,$item_per_page,10,'page_no',true);
			
			$sql 	= 'SELECT id,img_server, img_url,item_id, title, des, time, user_name, user_id, img_server FROM `item_image`';
			$sql    .= $search_value.' ORDER BY id ASC '.$limit;			
			
			$item_ids='';
			
			$re=DB::query($sql);
			
			if($re){
				while ($row=mysql_fetch_assoc($re)){
					$row['link_gallery'] = WEB_DIR.AZRewrite::formatUrl('?page=gallery&user_id='.$row['user_id'].'&year='.date('Y',$row['time']).'&month='.(int)date('m',$row['time']).'&day='.(int)date('d',$row['time']));
					
					if(date('d.m.y',$row['time']) == date('d.m.y',TIME_NOW)){$row['time'] = date('H:i',$row['time']);}
					elseif(date('y',$row['time']) == date('y',TIME_NOW)){
						{$row['time'] = date('d/m H:i',$row['time']);}
					}
					else{$row['time'] = date('d/m/y H:i',$row['time']);}
					
					//if(!eregi('/',$row['img_url'])){
					//	$row['link_img']='http://'.CGlobal::$img_server[$row['img_server']].'uploaded/items/'.$row['img_url'];
					//}
					//else{
						$row['link_img']='http://'.CGlobal::$img_server[$row['img_server']].$row['img_url'];
					//}
					
					if($row['item_id']){
						$item_ids.=($item_ids?',':'').$row['item_id'];
						
						if(isset($row['name'])){
							$row['item_link'] = WEB_DIR.AZRewrite::formatUrl("?page=item_detail&id={$row['item_id']}&ebname=".AZLib::safe_title($row['name']));
							
							if(date('d.m.y',$row['created_time']) == date('d.m.y',TIME_NOW))
							{$row['created_time'] = date('H:i',$row['created_time']);}
							elseif(date('y',$row['created_time']) == date('y',TIME_NOW)){
								{$row['created_time'] = date('d/m H:i',$row['created_time']);}
							}
							else
							{$row['created_time'] = date('d/m/y H:i',$row['created_time']);}
							
						}
						else{
							$row['item_link'] 	 = WEB_DIR.AZRewrite::formatUrl("?page=item_detail&id={$row['item_id']}");
							$row['created_time'] ='';
						}
						$row['title'] = stripslashes($row['title']);
					}
					else{
						$row['title']		 = stripslashes($row['des']);
						$row['created_time'] ='';
						$row['item_link']    = '';	
					}
					
					$row['image_thumb']	=AZLib::getImageThumb($row['img_url'],110,0,1,$row["img_server"]);
					$row['image_max']	=AZLib::getImageThumbMax($row['img_url'],$row['title'],1,$row["img_server"]);
					
					$row['images_url_root']	= 'http://'.CGlobal::$img_server[$row['img_server']];
					
					$row['del_link'] = Url::build_all(array('chk_id','del_all','cmd','id'),'cmd=delete&id='.$row['id']);
					
					$items[$row['id']]=$row;
				}
				
				if($item_ids){
					$all_items=array();
					
					$sql 	= "SELECT id, name, created_time FROM item WHERE id IN($item_ids)";		
					$re = DB::query($sql);
					if($re){	
						while ($item=mysql_fetch_assoc($re)){	
							$all_items[$item['id']]	= $item;
						}
					}
					
					foreach ($items as &$item_img){
						if($item_img['item_id'] && isset($all_items[$item_img['item_id']])){
							$item_img['item_link'] = WEB_DIR.AZRewrite::formatUrl("?page=item_detail&id={$item_img['item_id']}&ebname=".AZLib::safe_title($all_items[$item_img['item_id']]['name']));
							
							if(date('d.m.y',$all_items[$item_img['item_id']]['created_time']) == date('d.m.y',TIME_NOW)){
								$item_img['created_time'] = date('H:i',$all_items[$item_img['item_id']]['created_time']);
							}
							elseif(date('y',$all_items[$item_img['item_id']]['created_time']) == date('y',TIME_NOW)){
								$item_img['created_time'] = date('d/m H:i',$all_items[$item_img['item_id']]['created_time']);
							}
							else{
								$item_img['created_time'] = date('d/m/y H:i',$all_items[$item_img['item_id']]['created_time']);
							}
						}
						else{
							$item_img['item_link']    = '';	
							$item_img['created_time'] ='';
						}
					}
				}
			}
		}
		else{$paging = '';}
						
		AZLib::my_setcookie("page_img_id", (Url::get("page_no")!=""?intval(Url::get("page_no")):1), 60*60*24*365 + TIME_NOW);		
		
		$display->add("uri",CGlobal::$request_uri);	
		
		$display->add("images_url_root",'http://'.CGlobal::$img_server[IMAGE_SERVER_NO]);
		$display->add('user_name',Url::get('user_name'));
		$display->add('item_title',Url::get('item_title'));
		$display->add('items',$items);
		$display->add('paging',$paging);
		$display->output('ManageImage');
		$this->endForm();
	}
}
?>